/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-SDL2/specific/controller/game_frame_events.h"
#include "PlanetWars2dRT-SDL2/utils/SDL_keyboard.h"


EXTERN_C_BEGIN


void
game_frame_event_manager(const SDL_Event* e,
			 game_state_t* a_state)
{
  if(e->type == SDL_QUIT)
    {
      game_state_set_quit(a_state, true);
    }
  else if(e->type == SDL_KEYUP)
    {
      if(!SDL_mod_state_current_is_ctrl())
	{
	  if(e->key.keysym.sym == SDLK_SPACE)
	    {
	      game_state_toogle_pause(a_state);
	    }
	  else if(SDL_keycode_is_minus(e->key.keysym.sym))
	    {
	      game_state_decrement_update_speed(a_state);
	    }
	  else if(SDL_keycode_is_plus(e->key.keysym.sym))
	    {
	      game_state_increment_update_speed(a_state);
	    }
	}
      else
	{
	  if(e->key.keysym.sym == SDLK_q)
	    {
	      game_state_set_quit(a_state, true);
	    }
	  else if(SDL_keycode_is_minus(e->key.keysym.sym))
	    {
	      /* TODO zoom out */
	    }
	  else if(SDL_keycode_is_plus(e->key.keysym.sym))
	    {
	      /* TODO zoom in */
	    }
	}
    }
  else if(e->type == SDL_MOUSEBUTTONDOWN)
    {
      if(game_state_get_selection_state(a_state) == SELECTION_STATE_NULL)
	{
	  if(game_state_get_selection_type(a_state) == SELECTION_2D_TYPE_RECTANGLE)
	    {
	      int x, y;
	      SDL_GetMouseState(&x, &y);
	      game_state_starts_rectangle_selection(a_state, x, y);
	    }
	}
    }
  else if(e->type == SDL_MOUSEMOTION)
    {
      if(game_state_get_selection_state(a_state) == SELECTION_STATE_NOT_FINAL)
	{
	  if(game_state_get_selection_type(a_state) == SELECTION_2D_TYPE_RECTANGLE)
	    {
	      int x, y;
	      SDL_GetMouseState(&x, &y);
	      game_state_moves_rectangle_selection(a_state, x, y);
	    }
	}
    }
  else if(e->type == SDL_MOUSEBUTTONUP)
    {
      if(game_state_get_selection_state(a_state) == SELECTION_STATE_NOT_FINAL)
	{
	  if(game_state_get_selection_type(a_state) == SELECTION_2D_TYPE_RECTANGLE)
	    {
	      int x, y;
	      SDL_GetMouseState(&x, &y);
	      game_state_ends_rectangle_selection(a_state, x, y);
	    }
	}
      else if(game_state_get_selection_state(a_state) == SELECTION_STATE_FINAL)
	{
	  int x, y;
	  SDL_GetMouseState(&x, &y);
	  game_state_move_selected_ships(a_state, x, y);
	}
    }
}

void
game_frame_events_manager(game_state_t* a_state)
{
  SDL_Event event;
  while(SDL_PollEvent(&event) != 0)
    {
      game_frame_event_manager(&event, a_state);
    }
}


EXTERN_C_END
